package com.plusmoney.approval.customer;

import com.mp.approvalreq.entity.common.Constants;
import com.plusmoney.mq.MQCustomer;
import com.plusmoney.util.Utils;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


class CustomerApproval {
    private static final Logger logger = LoggerFactory.getLogger(CustomerApproval.class);

    private static MQCustomer customer;

    static void start() {
        customer = new MQCustomer();
        customer.group(Constants.MQ_GROUP_NAME)
                .topic(Constants.MQ_APPROVAL_TOPIC)
                .bind();
        try {
            customer.subscribe()
                    .setMessageModel(MessageModel.CLUSTERING)
                    .setConsumeMessageBatchMaxSize(10)
                    .setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET)
                    .listener(new ApprovalListener());

            customer.start();

            logger.info("------CustomerApproval start------");
        } catch (MQClientException e) {
            logger.info("------CustomerApproval start fail ------");
            logger.error(e.getErrorMessage(), e);
        }
    }

    static void shutdown() {
        logger.info("------CustomerApproval shutdown------");
        if (Utils.isNotNull(customer)) {
            customer.shutdown();
        }
    }
}
